/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * function NestedInteger() {
 *
 *     Return true if this NestedInteger holds a single integer, rather than a nested list.
 *     @return {boolean}
 *     this.isInteger = function() {
 *         ...
 *     };
 *
 *     Return the single integer that this NestedInteger holds, if it holds a single integer
 *     Return null if this NestedInteger holds a nested list
 *     @return {integer}
 *     this.getInteger = function() {
 *         ...
 *     };
 *
 *     Set this NestedInteger to hold a single integer equal to value.
 *     @return {void}
 *     this.setInteger = function(value) {
 *         ...
 *     };
 *
 *     Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
 *     @return {void}
 *     this.add = function(elem) {
 *         ...
 *     };
 *
 *     Return the nested list that this NestedInteger holds, if it holds a nested list
 *     Return null if this NestedInteger holds a single integer
 *     @return {NestedInteger[]}
 *     this.getList = function() {
 *         ...
 *     };
 * };
 */
/**
 * @param {string} s
 * @return {NestedInteger}
 */
 var deserialize = function(s) {
  if(s[0] !== '[') {
      let ret = new NestedInteger();
      ret.setInteger(parseInt(s, 10));
      return ret;
  }
  let i = 1;
  let l = s.length - 1;
  let ret = new NestedInteger();
  while(i < l) {
      if(s[i] === '[') {
          let cnt = 1;
          let g = i + 1;
          while(true) {
              if(s[g] === '[') {
                  cnt++;
              } else if (s[g] === ']') {
                  cnt--;
              }
              if(cnt === 0) {
                  break;
              }
              g++;
          }
          ret.add(deserialize(s.slice(i, g + 1)));
          i = g + 1;
      } else if (s[i] === ',') {
          i++;
      } else {
          let g = i + 1;
          while(s[g] !== ',' && s[g] !== ']') {
              g++;
          }
          let val = new NestedInteger();
          val.setInteger(parseInt(s.slice(i, g), 10))
          ret.add(val);
          i = g;
      }
  }
  return ret;
};